const urlParams = new URLSearchParams(window.location.search);
console.log(urlParams)
const id = urlParams.get('id');
new Vue({
    el:'#updateProduct',
    data:{
        infoFlag:false,
        infoText:'',
        image:'http://192.168.2.106:8848/images/',
        product:{
            id:'',
            name:'',
            fileName:'',
            description:'',
            price:'',
            stock:'',
            categoryLevel1Id:'',
            categoryLevel2Id:'',
            categoryLevel3Id:'',
            categoryLevel1Name:'',
            categoryLevel2Name:'',
            categoryLevel3Name:'',
            isDelete:'',
            creationDate:''
        },
        firstLevel:[],
        secondLevel:[],
        thirdLevel:[],
        updateImg:null,
        initName:''
    },
    methods:{
        getUpdateProduct()
        {
            axios.get('/api/admin/getProductById',
                {
                    params:{
                        id
                    }
                }).then(rs=>{
                this.product=rs.data.product
                this.image+=this.product.fileName
                this.initName=rs.data.product.name
            })
        },
        getUpdateLevel1()
        {
            this.firstLevel=[]
            axios.get('/api/admin/getUpdateLevel',
                {
                    params:{
                        type:1,
                        id:1
                    }
                }).then(rs=>{
                    this.firstLevel=rs.data.list
            })
        },
        getUpdateLevel2()
        {
            this.secondLevel=[]
            axios.get('/api/admin/getUpdateLevel',
                {
                    params:{
                        type:2,
                        id:this.product.categoryLevel1Id
                    }
                }).then(rs=>{
                this.secondLevel=rs.data.list
                let f1=false;
                this.secondLevel.forEach((p,index)=>{
                    if(p.id===this.product.categoryLevel2Id)
                    {
                        f1=true;
                    }
                })
                if(!f1)
                {
                    this.product.categoryLevel2Id=0
                    this.product.categoryLevel3Id=0
                }
            })
        },
        getUpdateLevel3()
        {
            this.thirdLevel=[]
            axios.get('/api/admin/getUpdateLevel',
                {
                    params:{
                        type:3,
                        id:this.product.categoryLevel2Id
                    }
                }).then(rs=>{
                this.thirdLevel=rs.data.list
            })
        },
        getFile(event)
        {
            this.updateImg=event.target.files[0]
        },
        updateSave()
        {
            if(this.product.name==="")
            {
                this.infoFlag=true
                this.infoText="商品名不能为空"
                return
            }
            if(this.product.price==='')
            {
                this.infoFlag=true
                this.infoText="单价不能为空"
                return
            }
            if(this.product.stock==='')
            {
                this.infoFlag=true
                this.infoText="库存不能为空"
                return
            }
            if(isNaN(this.product.price))
            {
                this.infoFlag=true
                this.infoText="单价为数字类型"
                return
            }
            if(isNaN(this.product.stock))
            {
                this.infoFlag=true
                this.infoText="库存为数字类型"
                return
            }
            if(this.secondLevel.length!==0 && this.product.categoryLevel2Id===0)
            {
                this.infoFlag=true
                this.infoText="该一级分类下存在子级分类，不能添加"
                return
            }
            if(this.thirdLevel.length!==0 &&  this.product.categoryLevel3Id===0)
            {
                this.infoFlag=true
                this.infoText="该二级分类下存在子级分类，不能添加"
                return
            }

            axios.get('/api/admin/checkProductName',{
                params:{
                    name:this.product.name,
                    categoryLevel1Id:this.product.categoryLevel1Id,
                    categoryLevel2Id:this.product.categoryLevel2Id,
                    categoryLevel3Id:this.product.categoryLevel3Id,
                }
            }).then(rs=>{
                    if(rs.data.isExist==="yes"&&rs.data.product.name!==this.initName)
                    {
                        this.infoText="商品名已存在"
                        this.infoFlag=true
                    }
                    else {
                        const formData = new FormData();
                        if(this.updateImg==null)
                        {
                            formData.append("fileImg",new Blob(),"")
                        }else{
                            formData.append('fileImg', this.updateImg);
                        }
                        formData.append('product', JSON.stringify(this.product)); // 转换为字符串


                        axios.post('/api/admin/updateProduct', formData, {
                            headers: {
                                'Content-Type': 'multipart/form-data'
                            }
                        }).then(rs=>{
                            this.infoFlag=true
                            if(rs.data.isUpdate==="success")
                            {
                                this.infoText="修改成功"
                                setTimeout(()=>{
                                    window.location="/api/ProductManage.html"
                                },500)
                            }
                            if(rs.data.isUpdate==="fail")
                            {
                                this.infoText="修改出错了"
                            }
                            if(rs.data.info==="noImg")
                            {
                                this.infoText="商品图是必须是图片格式"
                            }
                            if(rs.data.info==="outSize")
                            {
                                this.infoText="商品图不能超过5MB"
                            }
                        })
                    }
            })
        },
        closeInfo()
        {
            this.infoFlag=false
        },
    },
    created()
    {
        this.getUpdateProduct()
        this.getUpdateLevel1()
        setTimeout(()=>{
            this.getUpdateLevel2()
        },500)
        setTimeout(()=>{
                this.getUpdateLevel3()
        },500
        )
    }
})